package com.future.mayi.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.adapter.AddressAdapter;
import com.future.mayi.app.App;
import com.future.mayi.model.AddressModel;
import com.future.mayi.model.interfaces.AddressListener;
import com.future.mayi.response.AddressResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class AddressActivity extends BaseTitleActivity implements AdapterView.OnItemClickListener, AddressListener {

    @BindView(R.id.list_view)
    ListView listView;
    private List<AddressModel> list = null;

    private AddressAdapter adapter;

    private boolean isSelect;

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("收货地址");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_address);
    }

    @Override
    protected void initView() {

        list = new ArrayList<>(1);
        adapter = new AddressAdapter(this, R.layout.layout_address_item, list);
        adapter.setListener(this);
        listView.setAdapter(adapter);
    }

    @Override
    protected void initDates() {
        isSelect = getIntent().getBooleanExtra("isSelect", false);
        if (isSelect)
            listView.setOnItemClickListener(this);
        refresh();
    }

    @Override
    protected void initListener() {
        findViewById(R.id.tv_enter).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.tv_enter) {
            startActivityForResult(new Intent(this, AddAddressActivity.class), 210);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == 210 && data != null) {
            AddressModel model = (AddressModel) data.getSerializableExtra("model");
            showLoadingDialog();
            changeAddressModel(model);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        AddressModel model = list.get(position);
        Intent intent = new Intent();
        intent.putExtra("model", model);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @Override
    public void clickTop(AddressModel item) {
        showLoadingDialog();
        item.setIsdefault(item.getIsdefault() == 1 ? 0 : 1);
        changeAddressModel(item);
    }

    @Override
    public void clickDelete(AddressModel item) {
        showLoadingDialog();
        deleteAddressModel(item);
    }

    private void refresh() {
        new HttpRequest<AddressResponse>()
                .with(this)
                .addParam("token", App.getToken(mContext))
                .setApiCode(ApiCst.getAddressList)
                .setListener(new HttpRequest.OnNetworkListener<AddressResponse>() {
                    @Override
                    public void onSuccess(AddressResponse response) {
                        adapter.updateList(response.getList());
                        dissmissLoadingDialog();
                    }

                    @Override
                    public void onFail(String message, AddressResponse response) {
                        toast(message);
                        dissmissLoadingDialog();
                    }
                })
                .start(new AddressResponse());
    }

    private void deleteAddressModel(AddressModel item) {
        new HttpRequest<DefaultResponse>()
                .with(this)
                .addParam("id", item.getAddress_id())
                .addParam("token", App.getToken(mContext))
                .setApiCode(ApiCst.delAddress)
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        refresh();
                        toast(response.message);
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        toast(message);
                        dissmissLoadingDialog();
                    }
                })
                .start(new DefaultResponse());
    }

    private void changeAddressModel(AddressModel item) {

        String region = null;
        if (item.getArea() != null) {
            region = item.getArea().getProvince() + "," + item.getArea().getCity() + "," + item.getArea().getRegion();
        }

        new HttpRequest<DefaultResponse>()
                .with(this)
                .addParam("id", item.getAddress_id() == 0 ? null : item.getAddress_id())
                .addParam("name", item.getName())
                .addParam("phone", item.getPhone())
                .addParam("location", item.getLocation())
                .addParam("detail", item.getDetail())
                .addParam("isdefault", item.getIsdefault())
                .addParam("token", App.getToken(mContext))
                .addParam("region", region)
                .setApiCode(item.getAddress_id() == 0 ? ApiCst.addAddress : ApiCst.editAddress)
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        refresh();
                        toast(response.message);
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        toast(message);
                        dissmissLoadingDialog();
                    }
                })
                .start(new DefaultResponse());
    }

}
